package com.rongshu.utils;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.rongshu.view.login.entity.PasswordLogin;
import com.rongshu.view.userInfo.entity.PostVipAuthentication;
import com.utils.LogUtil;
import com.utils.SPUtils;
import com.utils.constants.SPKey;

public class UserInfoUtils {

    /**
     * 保存用户信息
     */
    public static void saveUserInfo(PasswordLogin passwordLogin) {
        String json = new Gson().toJson(passwordLogin.getResponse());
        SPUtils.putPreferences(SPKey.USER_INFO, json);
    }

    /**
     * 移除用户信息
     */
    public static void removeUserInfo() {
        SPUtils.removePreferences(SPKey.USER_INFO);
    }

    /**
     * 获取用户信息
     */
    public static PasswordLogin.ResponseBean getUserInfo() {
        String json = SPUtils.getPreferences(SPKey.USER_INFO, null);
        PasswordLogin.ResponseBean userInfo = null;
        if (!TextUtils.isEmpty(json)) {
            userInfo = new Gson().fromJson(json, PasswordLogin.ResponseBean.class);
        }
        return userInfo;
    }

    /**
     * 获取用户id
     */
    public static int getUserId() {
        int user_id = 0;
        PasswordLogin.ResponseBean userInfo = UserInfoUtils.getUserInfo();
        if (userInfo != null) {
            user_id = userInfo.getUser_id();
        }

        return user_id;
    }

    /**
     * 获取用户头像
     */
    public static String getAvatar() {
        String avatar = "";
        PasswordLogin.ResponseBean userInfo = UserInfoUtils.getUserInfo();
        if (userInfo != null) {
            avatar = userInfo.getAvatar();
        }

        return avatar;
    }

    /**
     * 获取用户vip
     */
    public static boolean getVip() {
        boolean vip = false;
        PasswordLogin.ResponseBean userInfo = UserInfoUtils.getUserInfo();
        if (userInfo != null) {
            vip = userInfo.isVip();
        }

        return vip;
    }

    /**
     * 获取用户笔名
     */
    public static String getBiName() {
        String bi_name = "";
        PasswordLogin.ResponseBean userInfo = UserInfoUtils.getUserInfo();
        if (userInfo != null) {
            bi_name = userInfo.getBi_name();
        }

        return bi_name;
    }

    /**
     * 保存Registration Id
     */
    public static void saveRegistrationId(String RegistrationId) {
        SPUtils.putPreferences(SPKey.REGISTRATIONID, RegistrationId);
    }

    /**
     * 获取Registration Id
     */
    public static String getRegistrationId() {
        return SPUtils.getPreferences(SPKey.REGISTRATIONID, null);
    }

    /**
     * 保存今日是否第一次登录
     */
    public static void saveFirstLogInExp(boolean isFirst) {
        //如果昨天的登录存在，首先清空
        String yesterday = DateUtils.getYesterday(DateUtils.YYYY_MM_DD);
        if (!TextUtils.isEmpty(yesterday)) {
            SPUtils.removePreferences(yesterday);
        }
        //保存当天
        String today = DateUtils.getToday(DateUtils.YYYY_MM_DD);
        SPUtils.putPreferences(today, isFirst);
    }

    /**
     * 获取今日是否第一次登录
     */
    public static boolean getFirstLoginExp() {
        String key = DateUtils.getToday(DateUtils.YYYY_MM_DD);
        return SPUtils.getPreferences(key, true);
    }

    /**
     * 获取资料认证信息
     */
    public static PostVipAuthentication getVipAuthentication() {
        String content = SPUtils.getPreferences(SPKey.MATERIALSAUTHENTICATION, null);
        LogUtil.i("getVipAuthentication", content);
        PostVipAuthentication vipAuthentication = null;
        if (!TextUtils.isEmpty(content)) {
            vipAuthentication = new Gson().fromJson(content, PostVipAuthentication.class);
        }
        return vipAuthentication;
    }
}
